Tweet.isSensitive   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
import {Twitter} from 'twit';
2 4
import Helper from '../Helper';
3 4
import {ReTweetableAbstract, Validation} from '../ReTweetable';
4 4
import TweetUser, {TweetUserConfig} from './TweetUser';
5
6
export type TweetConfig = {
7
    minFavs: number,
8
    minFavsToFollowers: number,
9
    hashtagsLimit: number,
10
    userConfig: TweetUserConfig,
11
    wordBlocklist?: string[]
12
};
13
14 4
export default class Tweet extends ReTweetableAbstract {
15
    rawTweet: Twitter.Status;
16
    tweetUser: TweetUser;
17
    config: TweetConfig;
18
19
    constructor(rawTweet: Twitter.Status, config: TweetConfig) {
20 42
        super();
21 42
        this.rawTweet = rawTweet;
22 42
        this.tweetUser = new TweetUser(this.rawTweet.user, config.userConfig);
23 42
        this.config = config;
24
    }
25
26
    isRetweet(): boolean {
27 20
        return this.rawTweet.retweeted || Helper.objectExists(this.rawTweet.retweeted_status);
28
    }
29
30
    isReply(): boolean {
31 15
        return Helper.objectExists(this.rawTweet.in_reply_to_screen_name)
32
        || Helper.objectExists(this.rawTweet.in_reply_to_status_id)
33
        || Helper.objectExists(this.rawTweet.in_reply_to_status_id_str)
34
        || Helper.objectExists(this.rawTweet.in_reply_to_user_id)
35
        || Helper.objectExists(this.rawTweet.in_reply_to_user_id_str);
36
    }
37
38
    isSensitive(): boolean {
39 14
        return Helper.objectExists(this.rawTweet.possibly_sensitive) && this.rawTweet.possibly_sensitive;
40
    }
41
42
    hasMinFavs(): boolean {
43 12
        return Helper.objectExists(this.rawTweet.favorite_count) && this.rawTweet.favorite_count >= this.config.minFavs;
44
    }
45
46
    hasMinFavsToFollowersRatio(): boolean {
47 11
        return Helper.objectExists(this.rawTweet.favorite_count) && this.rawTweet.favorite_count / this.rawTweet.user.followers_count >= this.config.minFavsToFollowers;
48
    }
49
50
    hasTooManyHashtags(): boolean {
51 10
        return this.rawTweet.entities.hashtags.length > this.config.hashtagsLimit;
52
    }
53
54
    isWithheld(): boolean {
55 9
        return Helper.objectExists(this.rawTweet.withheld_copyright) && this.rawTweet.withheld_copyright;
56
    }
57
58
    hasBlockListedWord(): boolean {
59 10
        if (!Helper.objectExists(this.config.wordBlocklist)
60
        || this.getText().length === 0) {
61 7
            return false;
62
        }
63
64 3
        const tweetText = this.getText().toLowerCase();
65
66 4
        return this.config.wordBlocklist.some(blockListedWord => tweetText.includes(blockListedWord.toLowerCase()));
67
    }
68
69
    getText(): string {
70 15
        const text = this.rawTweet.truncated ? this.rawTweet.full_text : this.rawTweet.text;
71
72 15
        if (text) {
73 8
            return text;
74
        }
75
76 7
        return '';
77
    }
78
79
    getRetweetValidations(): Validation[] {
80 15
        return [
81
            {
82 15
                validate: () => this.isRetweet(),
83 2
                message: () => 'Tweet is retweet'
84
            },
85
            {
86 13
                validate: () => this.isReply(),
87 1
                message: () => 'Tweet is reply'
88
            },
89
            {
90 12
                validate: () => this.isSensitive(),
91 2
                message: () => 'Tweet is sensitive'
92
            },
93
            {
94 10
                validate: () => !this.hasMinFavs(),
95 1
                message: () => 'Tweet does not have min favs'
96
            },
97
            {
98 9
                validate: () => !this.hasMinFavsToFollowersRatio(),
99 1
                message: () => 'Tweet does not have min favs to followers'
100
            },
101
            {
102 8
                validate: () => this.hasTooManyHashtags(),
103 1
                message: () => 'Tweet has too many hashtags'
104
            },
105
            {
106 7
                validate: () => this.isWithheld(),
107 1
                message: () => 'Tweet is withheld'
108
            },
109
            {
110 6
                validate: () => this.hasBlockListedWord(),
111 1
                message: () => 'Tweet has block listed word in it'
112
            },
113
            {
114 5
                validate: () => !this.tweetUser.isReTweetable(),
115 1
                message: () => this.tweetUser.retweetError
116
            },
117
        ];
118
    }
119
}